/**
 * anis.darwich
 */

package clubhouse.communication;

import java.util.Vector;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import clubhouse.motioncommand.MotionCommand;
import clubhouse.motioncommand.NodeControl;

public class ReceptionFifo {
	
	private final static Logger log = Logger.getLogger(ReceptionFifo.class);
	
	private Vector<MotionCommand> motionAck;
	private Vector<NodeControl> nodeMeasurement;
	
	public ReceptionFifo() {
		PropertyConfigurator.configure("log4j.properties");
		this.motionAck = new Vector<MotionCommand>(100,10);
		this.nodeMeasurement = new Vector<NodeControl>(100,10);
	}
	
	protected Object getLock() {
		return this;
	}
	
	public void rxNodeMeasurement(NodeControl msg) {
		synchronized(this.getLock()) {
			this.nodeMeasurement.add(msg);
			log.debug("Added "+ msg.toHexPrint() + " to nodeMeasurement\n");
		}
	}
	
	public void rxMotionAck(MotionCommand msg) {
		synchronized(this.getLock()) {
			this.motionAck.add(msg);
			log.debug("Added "+ msg.toHexPrint() + " to motionAck\n");
		}
	}
	
	public NodeControl getNodeMeasurement() {
		synchronized(this.getLock()) {
			if (this.nodeMeasurement.size() != 0) {
				NodeControl tmp = this.nodeMeasurement.firstElement();
				this.nodeMeasurement.remove(0);
				log.debug("Retrieved "+ tmp.toHexPrint() + " from nodeMeasurement\n");
				return tmp;
			} else {
				return null;
			}
		}
	}
	
	public int nodeMeasurementAvailable() {
		return this.nodeMeasurement.size();
	}
	
	public MotionCommand getMotionAck() {
		synchronized(this.getLock()) {
			if (this.motionAck.size() != 0) {
				MotionCommand tmp = this.motionAck.firstElement();
				this.motionAck.remove(0);
				log.debug("Retrieved "+ tmp.toHexPrint() + " from motionAck\n");
				return tmp;
			} else {
				return null;
			}
		}
	}
	
	public int motionAckAvailable() {
		return this.motionAck.size();
	}
	
	public Vector<NodeControl> getReceivedLandmarks() {
		Vector<NodeControl> tmp = new Vector<NodeControl>(100,10);
		NodeControl tmpMsg;
		while ((tmpMsg = getNodeMeasurement()) != null) {
			tmp.add(tmpMsg);
		}
		return tmp;
	}
}
